package com.zynn.message.queue.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessagePostProcessor;
import org.springframework.stereotype.Service;

/**
 * @author admin
 *
 * activeMQ 消息发送类
 */
@Service
public class SenderService {

    @Autowired
    private JmsTemplate jmsTemplate;

    /***
     *  发送消息
     * @param destination   队列名称
     * @param message       发送的报文
     */
    public void send(final String destination, final String message) {
        this.jmsTemplate.convertAndSend(destination, message);
    }

    /***
     *  发送消息
     * @param destination   队列名称
     * @param message       发送的报文
     */
    public void send(final String destination, final Object message) {
        this.jmsTemplate.convertAndSend(destination, message);
    }

    public void send(final String destination, final Object message,ScheduleMessagePostProcessor processor) {
        this.jmsTemplate.convertAndSend(destination, message,processor);
    }

    /**
     * 延迟推送
     * 需要配置 MQ:schedulerSupport="true"
     * 参考资料：https://www.2cto.com/kf/201712/707023.html
     *
     * @param destination          队列名
     * @param text                 推送的内容
     * @param messagePostProcessor 延迟推送策略
     */
    public void sendDelay(final String destination, final Object text, MessagePostProcessor messagePostProcessor) {
        jmsTemplate.convertAndSend(destination, text, messagePostProcessor);
    }
}
